package cc.inik.sqlite;

import androidx.annotation.ContentView;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn_insert;
    private Button btn_delete;
    private Button btn_update;
    private Button btn_query;
    private SQLiteDatabase db;
    private TextView tv;
    private int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.bindView();
        DBHelper dbHelper = new DBHelper(this,"my.db",null,1);

        btn_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                //values.put("uid",1);
                values.put("uname","张三"+i);
                values.put("pwd","123456");
                db.insert("person",null,values);
                i++;
                Toast.makeText(MainActivity.this,"插入成功",Toast.LENGTH_SHORT).show();
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.delete("person",null,null);
            }
        });
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values = new ContentValues();
                //values.put("uname","李四");
                db.update("person",values,null,null);
            }
        });
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuilder re = new StringBuilder();
                 Cursor cursor = db.query("person",null,null,null,null,null,null);
                if(cursor.moveToFirst()){
                    do{
                       @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("uid"));
                       @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("uname"));
                       @SuppressLint("Range") String pwd = cursor.getString(cursor.getColumnIndex("pwd"));
                        re.append("uid:"+id+"\tname:"+name+"\tpwd:"+pwd+"\n");
                    }while (cursor.moveToNext());
                }
                cursor.close();
                tv.setText(re.toString());
            }
        });
    }
    protected  void bindView(){
        this.btn_insert = findViewById(R.id.btn_insert);
        this.btn_query = findViewById(R.id.btn_query);
        this.btn_delete = findViewById(R.id.btn_delete);
        this.btn_update = findViewById(R.id.btn_update);
        this.tv = findViewById(R.id.res_tv);
    }

}